package com.qiyuesuo.utils;

import com.qiyuesuo.exception.code.BaseResponseCode;
import com.qiyuesuo.exception.code.ResponseCodeInterface;
import lombok.Data;

/**
 * @ClassName: DataResult
 * TODO:前后端分离，接口编程
 * @Author: ZNQ
 * @UpdateUser: ZNQ
 * @Version: 0.0.1
 */
@Data
public class DataResult <T>{
    /**
     * 请求响应code， 0表示请求成功 其它表示失败
     */
    private int code;

    /**
     * 响应客户端的提示
     */
    private String msg;

    /**
     * 响应客户端内容
     */
    private T data;

    public DataResult(int code, T data) {
        this.code = code;
        this.data = data;
        this.msg=null;
    }

    public DataResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public DataResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data=null;
    }

    public DataResult() {
        this.code=BaseResponseCode.SUCCESS.getCode();
        this.msg=BaseResponseCode.SUCCESS.getMsg();
        this.data=null;
    }

    public DataResult(T data) {
        this.data = data;
        this.code=BaseResponseCode.SUCCESS.getCode();
        this.msg=BaseResponseCode.SUCCESS.getMsg();
    }

    public DataResult(ResponseCodeInterface responseCodeInterface) {
        this.data = null;
        this.code = responseCodeInterface.getCode();
        this.msg = responseCodeInterface.getMsg();
    }

    public DataResult(ResponseCodeInterface responseCodeInterface, T data) {
        this.data = data;
        this.code = responseCodeInterface.getCode();
        this.msg = responseCodeInterface.getMsg();
    }

    public static DataResult success(){
        return new DataResult();
    }

    public static <T>DataResult success(T data){
        return new DataResult(data);
    }

    public static <T>DataResult getResult(int code,String msg,T data){
        return new DataResult(code,msg,data);
    }

    public static DataResult getResult(int code,String msg){
        return new DataResult(code,msg);
    }

    public static DataResult getResult(BaseResponseCode responseCode){
        return new DataResult(responseCode);
    }

    public static <T>DataResult getResult(BaseResponseCode responseCode, T data){
        return new DataResult(responseCode,data);
    }

}
